#!/usr/bin/perl -w

use strict;
use warnings;

open (IN, "<$ARGV[0]");

open (OUT ,">$ARGV[1]");
#open (OUT_id, ">$ARGV[2]");
#open (OUT_path, ">$ARGV[3]");

my %options;
my $srType;
my $srDir="sr";
`mkdir $srDir`;
#my $fileNum = 0;
#print OUT `pwd`
while(<IN>){
    chop;
    if ($_ =~ /^\*\+\*/){
	if ($_ =~ /params\.([\w]+?)="([^\"]+?)"/){
	    $options{$1} = $2;
	}
    }
}
# set essentional links for files
my $labelSRType = "searchResultsType";
my $labelSRFile = "searchResultsFile";
my $labelSRFN = "searchResultsFileName";

if (exists $options{$labelSRType}){
    if (($options{$labelSRType}) eq "single"){
	$srType = "single";
	if (exists $options{$labelSRFile}){
	    my $sr = $options{$labelSRFile};
	    my $link_sr = "temp.pep.xml";
	    
	    `ln -s $sr $srDir/$link_sr`;
	}else{
	    die "ERROR: NOT specify the search result file!\n";
	}
    }elsif ($options{$labelSRType} eq "fileSet"){
	$srType = "fileSet";
	if (exists $options{$labelSRFile}){
	    my $srFileSet = $options{$labelSRFile};
	    my $link_srFileSet = "temp.zip";

	    `ln -s $srFileSet $link_srFileSet`;
	    `unzip $link_srFileSet -d $srDir`;
	}else{
	    die "ERROR: NOT specify the search results file set!\n";
	}
    }else{
	die "ERROR: UNKNOWN search results file type: $srType!\n";
    }
}else{
    die "ERROR: NOT Specify searchResultsType!\n";
}

my $command = "chmod a+u $srDir; cd $srDir;xinteract -OPdNM -dDECOY -nR -p0 '*.pep.xml';zip output.zip interact.pep*; mv output.zip ../; cd .. ; rm -rf $srDir";
#print OUT $command;
my $run = `$command`;
#print OUT $run;
print OUT `cat output.zip`;

close IN;
close OUT;
